#include "StdAfx.h"
#include <iostream>
#include "Request.h"
#include "Word.h"
#include "stdlib.h"
#include "StrUtil.h"



CRequest::CRequest(const string& line)
{
	m_keys = CWord::MakeKeys(ParseLine(line));
}


CRequest::~CRequest(void)
{
	m_keys.clear();
}


int CRequest::ToInt(const string &str)
{
	char * pEnd;
	return (int)strtol(str.c_str(), &pEnd, 10);
}


string CRequest::ParseLine(const string& line)
{
	vector<string> vect = CStrUtil::Split(line, " ");
	if (vect.size() < 2)
	{
		cerr << "inupt format error" << endl;
		throw;
	}

	vector<string>::iterator itr = vect.begin();
	for(itr++; itr != vect.end(); itr++)
	{
		m_lengths.push_back(ToInt(*itr));
	}

	return vect[0];
}

const map<char, int> CRequest::GetKeys() const
{
	return m_keys;
}

bool CRequest::GetCandidates(
	vector<list<CWord*>> &wordList, 
	CDictionary *pDict)
{
	for(vector<int>::iterator itr = m_lengths.begin();
			itr != m_lengths.end(); itr++)
	{
		list<CWord*> tmpList;
		tmpList.clear();

		int count = pDict->GetCandidates(*itr, m_keys, tmpList);
		if (count == 0)
		{
			return false;
		}
		else
		{
			wordList.push_back(tmpList);
		}
	}
	return true;
}
